<?php

class m_lokasi extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_lokasi() {
        $sql = "SELECT COUNT(lokasi_cd)'total' FROM lokasi";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

     function get_count_all_lokasi_search($param) {
        $sql = "SELECT COUNT(lokasi_cd)'total' FROM lokasi WHERE lokasi_name LIKE ?";
        $query = $this->db->query($sql, $param);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }
    
    //cek data
    function get_count_all_lokasi_by_kode($params) {
        $sql = "SELECT COUNT(*) 'total' FROM lokasi WHERE lokasi_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_lokasi_by_kode_nama($params) {
        $sql = "SELECT COUNT(*) 'total' FROM lokasi WHERE lokasi_cd = ? AND lokasi_name = ? AND mdb =?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_lokasi_pagination($params) {
        $sql = "SELECT * FROM lokasi
                ORDER BY CAST(lokasi_cd AS UNSIGNED) ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

      function get_all_lokasi_pagination_search($params) {
        $sql = "SELECT * FROM lokasi
                WHERE lokasi_name LIKE ?
                ORDER BY CAST(lokasi_cd AS UNSIGNED) ASC         
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    function get_all_lokasi() {
        $sql = "SELECT * FROM lokasi
                ORDER BY CAST(lokasi_cd AS UNSIGNED) ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_lokasi_by_id($params) {
        $sql = "SELECT * FROM lokasi WHERE lokasi_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO lokasi VALUES (?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE lokasi SET lokasi_name = ?, mdb = ? WHERE lokasi_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM lokasi WHERE lokasi_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    function get_lokasi_barang() {
        $sql = "SELECT a.*, 
                COUNT(b.barang_id) AS 'total_barang', 
                SUM(b.barang_harga_beli) AS 'total_harga_beli', 
                SUM(b.barang_nilai_buku) AS 'total_nilai_buku' 
                FROM lokasi a
                LEFT JOIN barang b ON a.lokasi_cd = b.lokasi_cd
                GROUP BY a.lokasi_cd";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

}
